﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Innovation
{
    public class LitMaterial : DefaultMaterial
    {
        Vector3 ambientLightColor;
        Vector3 light1Color;
        Vector3 light1Direction;
        Vector3 light2Color;
        Vector3 light2Direction;
        Vector3 light3Color;
        Vector3 light3Direction;
        Vector3 cameraPosition;
        Vector3 cameraDirection;
        float specularity;

        public Vector3 AmbientLightColor
        {
            get { return ambientLightColor; }
            set { ambientLightColor = value; SetParameter("AmbientLightColor", value); }
        }

        public Vector3 Light1Color
        {
            get { return light1Color; }
            set 
            {
                Vector3[] newColors = new Vector3[3];
                newColors[0] = value;
                newColors[1] = Light2Color;
                newColors[2] = Light3Color;

                light1Color = value; 
                SetParameter("LightColor", newColors); 
            }
        }

        public Vector3 Light1Direction
        {
            get { return light1Direction; }
            set 
            {
                Vector3[] newDirections = new Vector3[3];
                newDirections[0] = value;
                newDirections[1] = Light2Direction;
                newDirections[2] = Light3Direction;

                light1Direction = value; 
                SetParameter("LightDirection", newDirections); 
            }
        }

        public Vector3 Light2Color
        {
            get { return light2Color; }
            set
            {
                Vector3[] newColors = new Vector3[3];
                newColors[0] = Light1Color;
                newColors[1] = value;
                newColors[2] = Light3Color;

                light2Color = value;
                SetParameter("LightColor", newColors);
            }
        }

        public Vector3 Light2Direction
        {
            get { return light2Direction; }
            set
            {
                Vector3[] newDirections = new Vector3[3];
                newDirections[0] = Light1Direction;
                newDirections[1] = value;
                newDirections[2] = Light3Direction;

                light2Direction = value;
                SetParameter("LightDirection", newDirections);
            }
        }

        public Vector3 Light3Color
        {
            get { return light3Color; }
            set
            {
                Vector3[] newColors = new Vector3[3];
                newColors[0] = Light1Color;
                newColors[1] = Light2Color;
                newColors[2] = value;

                light3Color = value;
                SetParameter("LightColor", newColors);
            }
        }

        public Vector3 Light3Direction
        {
            get { return light3Direction; }
            set
            {
                Vector3[] newDirections = new Vector3[3];
                newDirections[0] = Light1Direction;
                newDirections[1] = Light2Direction;
                newDirections[2] = value;

                light3Direction = value;
                SetParameter("LightDirection", newDirections);
            }
        }

        public Vector3 CameraPosition
        {
            get { return cameraPosition; }
            set { cameraPosition = value; SetParameter("CameraPosition", value); }
        }

        public Vector3 CameraDirection
        {
            get { return cameraDirection; }
            set { cameraDirection = value; SetParameter("CameraDirection", value); }
        }

        public float Specularity
        {
            get { return specularity; }
            set { specularity = value; SetParameter("Specularity", value); }
        }

        /*public override string Texture
        {
            get { return base.Texture; }
            set
            {
                base.Texture = value;
                //SetParameter("Texture", this.TextureValue);
                //((string[])this.MeshPart.Tag)[1] = value;
            }
        }*/
        
        public LitMaterial()
        {
            this.Effect = "Content/LitModel";
            this.AmbientLightColor = new Vector3(.46f, .46f, .46f);

            this.Light1Color = new Vector3(.65f, .65f, .65f);
            this.light2Color = new Vector3(.65f, .65f, .65f);
            this.Light3Color = new Vector3(.65f, .65f, .65f);
            this.Light1Direction = new Vector3(-.5f, -.5f, -.6f);
            this.Light2Direction = new Vector3(1, .75f, .4f);
            this.Light3Direction = new Vector3(.45f, .75f, .45f);

            this.Specularity = 500000;
        }

        public override void Update()
        {
            base.Update();

            if (Engine.Services.ContainsService(typeof(Camera)))
            {
                Camera camera = Engine.Services.GetService<Camera>();

                CameraPosition = camera.Transform.Position;

                Vector3 dir = camera.Transform.Position - camera.Target;
                dir.Normalize();

                CameraDirection = dir;
            }
        }

        public override void SaveSerializationData(SerializationData Data)
        {
            base.SaveSerializationData(Data);

            Data.AddData("LitMaterial.AmbientLightColor", AmbientLightColor);
            Data.AddData("LitMaterial.Light1Color", Light1Color);
            Data.AddData("LitMaterial.Light2Color", Light2Color);
            Data.AddData("LitMaterial.Light3Color", Light3Color);
            Data.AddData("LitMaterial.Specularity", Specularity);
        }

        public override void LoadFromSerializationData(SerializationData Data)
        {
            base.LoadFromSerializationData(Data);

            this.AmbientLightColor = Data.GetData<Vector3>("LitMaterial.AmbientLightColor");
            this.Light1Color = Data.GetData<Vector3>("LitMaterial.Light1Color");
            this.Light2Color = Data.GetData<Vector3>("LitMaterial.Light2Color");
            this.Light3Color = Data.GetData<Vector3>("LitMaterial.Light3Color");
            this.Specularity = Data.GetData<float>("LitMaterial.Specularity");
        }
    }
}
